<?php
namespace cubox\provider;

class Url{
    public static function base64Encode(string $url,bool $check = false):string{
        $str = str_replace(['/','+','='],['_','-',''],base64_encode($url));

        if($check){
            $str = substr(md5($str.'acdb'),0,10).$str;
        }

        return $str;
    }

    public static function base64Decode(string $url,bool $check = false):string{
        
        if($check){
            $md5 = substr($url,0,10);
           
            $url = substr($url,10);
            if ($md5!=substr(md5($url.'acdb'),0,10)){
                throw new \Exception('URL 错误');
            }
        }

        $str = str_replace(['_','-'],['/','+'],$url);

        if(strlen($str)%4){
            $str .= str_repeat('=',4-strlen($str)%4);
        }

        return base64_decode($str);
    }
}